/**
 * 
 */
package com.cqan.account.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.cqan.entity.pk.IdEntity;


/**
 * @Description TODO
 * @author wuhui
 * @date 2012-4-27下午6:23:52
 * 
 */
@Entity
//表名与类名不相同时重新定义表名.
@Table(name = "DMS_ROLE")
//默认的缓存策略.
@SuppressWarnings("serial")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Role extends IdEntity {

	private List<Department> departments;

	private boolean enabled;

	private String name;

	private List<Permission> permissions;
	
	private List<User> users;

	@ManyToMany(cascade = CascadeType.REFRESH, mappedBy = "roles",fetch = FetchType.LAZY)
	public List<Department> getDepartments() {
		return departments;
	}

	@Column(name="NAME")
	public String getName() {
		return name;
	}

	// 多对多定义
	@ManyToMany
	// 中间表定义,表名采用默认命名规则
	@JoinTable(name = "DMS_ROLE_PERMISSION", joinColumns = { @JoinColumn(name = "RID") }, inverseJoinColumns = { @JoinColumn(name = "PID") })
	// Fecth策略定义
	@Fetch(FetchMode.SUBSELECT)
	// 集合按id排序.
	@OrderBy("id")
	// 集合中对象id的缓存.
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Permission> getPermissions() {
		return permissions;
	}

	@ManyToMany(cascade = CascadeType.REFRESH, mappedBy = "roles",fetch = FetchType.LAZY)
	public List<User> getUsers() {
		return users;
	}

	@Column(name="ENABLED")
	public boolean isEnabled() {
		return enabled;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

}
